/**
 * 
 */
package com.acc.softmgr.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acc.softmgr.business.model.SysUser;
import com.acc.softmgr.business.model.SysUserExample;
import com.acc.softmgr.business.service.ISysUserService;
import com.acc.softmgr.common.constant.DeleteStatus;
import com.acc.softmgr.common.constant.enums.RestStatus;
import com.acc.softmgr.common.utils.date.DateBase;
import com.acc.softmgr.common.utils.string.StringUtil;
import com.acc.softmgr.common.web.DataGridObject;
import com.acc.softmgr.common.web.ResponseObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月12日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("sysUser")
public class SysUserController {

	@Autowired
	private ISysUserService sysUserService;

	@RequestMapping(value = "toSysUser", method = RequestMethod.GET)
	public String toSysUser() {
		return "sysUser/sysUser";
	}

	/**
	 * 账号密码验证
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "checkUser", method = RequestMethod.POST)
	public ResponseObject checkUser(SysUser user, HttpServletRequest request) {
		ResponseObject responseObject = new ResponseObject();
		SysUserExample example = new SysUserExample();
		example.createCriteria().andUsernameEqualTo(user.getUsername()).andPasswordEqualTo(user.getPassword());
		List<SysUser> list = sysUserService.selectByExample(example);
		if (list != null && list.size() > 0) {
			request.getSession().setAttribute("currentUser", list.get(0));
			responseObject.setStatus(RestStatus.EXIT_USER.code);
			responseObject.setMessage(RestStatus.EXIT_USER.message);
		} else {
			responseObject.setStatus(RestStatus.NO_EXIT_USER.code);
			responseObject.setMessage(RestStatus.NO_EXIT_USER.message);
		}
		return responseObject;
	}

	/**
	 * 查询列表页
	 * 
	 * @param username
	 * @param userEmail
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSysUsers", method = RequestMethod.GET)
	@ResponseBody
	public DataGridObject<SysUser> searchSysUsers(@Validated String username, String userEmail, Integer page,
			Integer rows, HttpServletRequest request, HttpServletResponse response) throws Exception {
		DataGridObject<SysUser> responseObject = new DataGridObject<SysUser>();
		SysUserExample example = new SysUserExample();
		if (!StringUtil.isEmpty(userEmail)) {
			example.getCriteria().andUser_emailEqualTo(userEmail);
		}
		if (!StringUtil.isEmpty(username)) {
			example.getCriteria().andUsernameEqualTo(username);
		}
		if (page == null || page == 0) {
			page = 1;
		}
		SysUser usernamesesion = (SysUser) request.getSession().getAttribute("currentUser");
		if (usernamesesion != null) {
			example.getCriteria().andCreatebyEqualTo(usernamesesion.getUsername());
		}

		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);

		List<SysUser> sysUsers = sysUserService.selectByExample(example);
		for (SysUser user : sysUsers) {
			user.setCreateTimestring(DateBase.formatDate(user.getCreatetime(), DateBase.DATE_PATTERN_DATETIME));
		}
		responseObject.setTotal(page * rows);
		responseObject.setRows(sysUsers);
		return responseObject;
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchPsUser", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObject searchUser(@PathVariable Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysUser sysUser = sysUserService.selectByPrimaryKey(id);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		responseObject.setData(sysUser);
		return responseObject;
	}

	/**
	 * 新增用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addSysUser", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject addSysUser(@Validated String username, String password, String user_email,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password) || StringUtil.isEmpty(user_email)) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");
		if (StringUtil.isEmpty(currentUser.getUsername())) {
			currentUser.setUsername("root");
		}
		SysUser sysUser = new SysUser();
		sysUser.setCreatetime(new Date());
		sysUser.setCreateby(currentUser.getUsername());
		sysUser.setPassword(password);
		sysUser.setUser_email(user_email);
		sysUser.setUsername(username);
		sysUser.setDeleted(DeleteStatus.FALSE);
		// -------------------
		SysUserExample example = new SysUserExample();
		example.getCriteria().andPasswordEqualTo(password);
		example.getCriteria().andUsernameEqualTo(username);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SysUser> sysUsers = sysUserService.selectByExample(example);
		if (sysUsers != null && sysUsers.size() > 0) {
			responseObject.setStatus(RestStatus.NOT_UNIQUE.code);
			responseObject.setMessage(RestStatus.NOT_UNIQUE.message);
			return responseObject;
		}
		// ----------判断唯一性------------
		sysUserService.insert(sysUser);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSysUser", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject editSysUser(@Validated Integer id, String username, String password, String user_email,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password) || StringUtil.isEmpty(user_email) || id == null
				|| id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysUser oldUser = sysUserService.selectByPrimaryKey(id);
		if (oldUser.getUsername().equals(username) && oldUser.getPassword().equals(password)) {
			oldUser.setUser_email(user_email);
			sysUserService.updateByPrimaryKey(oldUser);
			responseObject.setStatus(RestStatus.SUCCESS.code);
			responseObject.setMessage(RestStatus.SUCCESS.message);
			return responseObject;
		}
		// -------------------
		SysUserExample example = new SysUserExample();
		example.getCriteria().andPasswordEqualTo(password);
		example.getCriteria().andUsernameEqualTo(username);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SysUser> sysUsers = sysUserService.selectByExample(example);
		if (sysUsers == null || sysUsers.size() < 1) {
			responseObject.setStatus(RestStatus.NOT_UNIQUE.code);
			responseObject.setMessage(RestStatus.NOT_UNIQUE.message);
			return responseObject;
		}
		// ----------判断唯一性------------
		oldUser.setPassword(password);
		oldUser.setUsername(username);
		oldUser.setUser_email(user_email);
		sysUserService.updateByPrimaryKey(oldUser);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 删除用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSysUser", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject removeSysUser(@Validated Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysUser sysUser = sysUserService.selectByPrimaryKey(id);
		sysUser.setDeleted(DeleteStatus.TRUE);
		sysUserService.updateByPrimaryKey(sysUser);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}
}
